package com.qf.mybatisplus.utils.mail;


import com.sun.istack.ByteArrayDataSource;

import javax.activation.DataHandler;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;

/**
 * @description: Java发送邮件工具类
 * @projectName: customer-service-workorder
 * @package: com.zuma.cloud.utils.mail
 * @className: JavaMailUtils
 * @author: zdd
 * @date: 2023/10/23 上午10:42
 * @version: 1.0
 */
public class JavaMailUtils {

    public static Session createSession() {
        Properties pros = new Properties();
        pros.put("mail.smtp.host", "smtp.163.com");
        pros.put("mail.smtp.port", "25");
        pros.put("mail.smtp.auth", "true");
        pros.put("mail.smtp.starttls.enable", "true");

        //创建Session
        Session session = Session.getInstance(pros, new Authenticator() {
            // 发件人邮箱账号
            String userName = "zhu13213018164@163.com";
            // 授权码
            String password = "GUSYIWCJSPLGLAYU";

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, password);
            }
        });
        session.setDebug(true);
        return session;
    }

    public static void main(String[] args) {
       JavaMailUtils mailUtils = new JavaMailUtils();
        // 发送普通邮件
//        mailUtils.sendMail();
        // 发送带有附件的邮件
       JavaMailUtils.sendFileMail("");
    }

    /**
     * 发送普通邮件
     */
    public static void sendMail() {
        //1.创建Session
        try {
            Session session =JavaMailUtils.createSession();

            //2.创建邮件对象
            MimeMessage message = new MimeMessage(session);
            //设置邮件主题
            message.setSubject("这是一个测试邮件");
            //设置邮件内容
            message.setText("测试邮件");
            //设置发件人
            message.setFrom(new InternetAddress("zhu13213018164@163.com"));
            //设置收件人
            message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("1213639167@qq.com"));

            //3.发送邮件
            Transport.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 发送带有附件的邮件
     */
   public static void sendFileMail(String filePath) {
        try {
            //1.创建Session会话
            Session session =JavaMailUtils.createSession();

            //2.创建邮件对象
            MimeMessage message = new MimeMessage(session);
            message.setSubject("这是一个带有附件的测试邮件");
            // 发件人邮箱账号
            message.setFrom(new InternetAddress("zhu13213018164@163.com"));
            // 收件人邮箱账号
            message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("1213639167@qq.com"));

            //设置正文
            BodyPart textPart = new MimeBodyPart();
            //设置正文内容并标明含有html标签
            textPart.setContent("带有<b>附件</b>的邮件", "text/html;charset=utf-8");

            //设置附件
            BodyPart filePart = new MimeBodyPart();
            filePart.setFileName("testGdShow.xlsx");
            filePart.setDataHandler(
                    new DataHandler(
                            new ByteArrayDataSource(
                                    // 附件文件路径
                                    Files.readAllBytes(Paths.get(filePath)), "application/octet-stream")));

            //整合正文和附件
            Multipart part = new MimeMultipart();
            part.addBodyPart(textPart);
            part.addBodyPart(filePart);

            //添加正文和邮件
            message.setContent(part);
            //3.发送邮件
            Transport.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
